 package sza.oracle.apps.ap.oie.webui;
 
 import java.sql.CallableStatement;
 import java.sql.SQLException;
import java.sql.Types;

import java.text.DecimalFormat;
 import oracle.apps.ap.oie.server.GeneralInfoAMImpl;
 import oracle.apps.ap.oie.server.ReceiptBasedLinesVOImpl;
 import oracle.apps.ap.oie.server.ReceiptBasedLinesVORowImpl;
 import oracle.apps.ap.oie.server.WebExpensesAMImpl;
 import oracle.apps.ap.oie.webui.ButtonsCO;
 import oracle.apps.ap.oie.webui.NavigationUtility;
 import oracle.apps.fnd.common.MessageToken;
 import oracle.apps.fnd.common.VersionInfo;
 import oracle.apps.fnd.framework.OAApplicationModule;
 import oracle.apps.fnd.framework.OAException;
 import oracle.apps.fnd.framework.OAViewObject;
 import oracle.apps.fnd.framework.server.OADBTransactionImpl;
 import oracle.apps.fnd.framework.webui.OAPageContext;
 import oracle.apps.fnd.framework.webui.beans.OAWebBean;
 import oracle.apps.fnd.framework.webui.beans.nav.OANavigationBarBean;
 import oracle.cabo.ui.UIConstants;
 import oracle.jbo.Row;
 import oracle.jbo.domain.Number;
 import oracle.jbo.server.DBTransaction;
 import oracle.jdbc.OracleCallableStatement;

import sza.oracle.apps.ap.oie.budget.server.TravelExpenseCategoryVOImpl;
import sza.oracle.apps.ap.oie.server.ExpenseTypeHierarchyVOImpl;
import sza.oracle.apps.ap.oie.utility.Arith;
 import sza.oracle.apps.ap.oie.utility.SzaOIEUtil;
 
 public class SzaButtonsCO extends ButtonsCO
 {
   public static final String RCS_ID = "$Header$";
   public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header$", "%packagename%");
     private String attribute1 = "";
     private String attribute2 = "";
     private String attribute11 = "";
     private String attribute12 = "";
     private String attribute13 = "";
     private String attribute14 = "";
     private String attribute15 = "";
 
   public void processRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaButtonsCO.processRequest";
     OAApplicationModule am = pageContext.getApplicationModule(webBean);
 
     LogMessage = "SZA SzaButtonsCO.processRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     super.processRequest(pageContext, webBean);
 
     OANavigationBarBean localOANavigationBarBean = (OANavigationBarBean)webBean.findIndexedChildRecursive("OIENavBar");
 
     if (localOANavigationBarBean != null)
     {
       localOANavigationBarBean.setCSSClass("xc");
     }
 
     OAWebBean OIENavBar = pageContext.getRootWebBean().findChildRecursive("OIENavBar");
 
     if (OIENavBar != null)
     {
       OIENavBar.setAttributeValue(UIConstants.STYLE_CLASS_ATTR, "xcxc");
     }
 
     LogMessage = "SZA SzaButtonsCO.processRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
 
   public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaButtonsCO.processFormRequest";
     OAApplicationModule am = pageContext.getApplicationModule(webBean);
 
     LogMessage = "SZA SzaButtonsCO.processFormRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String IsExpenseRecognitionReport = (String)pageContext.getTransactionValue("IsExpenseRecognitionReport");
 
     LogMessage = "SZA SzaButtonsCO.processFormRequest - IsExpenseRecognitionReport:" + IsExpenseRecognitionReport;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     WebExpensesAMImpl webexpensesamimpl = (WebExpensesAMImpl)pageContext.getApplicationModule(webBean);
 
     Number intReportHeaderId = webexpensesamimpl.getReportHeaderId();
     String ReportHeaderId = (String)pageContext.getTransactionValue("ReportHeaderId");
 
     String strExpenseReportNumber = webexpensesamimpl.getInvoiceNum();
 
     String SzaDeptApprover = pageContext.getParameter("SzaDeptApprover");
     LogMessage = "SZA SzaButtonsCO.processFormRequest - SzaDeptApprover:" + SzaDeptApprover;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String CurrentPage = NavigationUtility.getCurrentPage(pageContext);
     LogMessage = "SZA SzaButtonsCO.processFormRequest - CurrentPage:" + CurrentPage;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String source = pageContext.getParameter("source");
     LogMessage = "SZA SzaButtonsCO.processFormRequest - source:" + source;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String event = pageContext.getParameter("event");
     LogMessage = "SZA SzaButtonsCO.processFormRequest - event:" + event;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String TemplateId = (String)pageContext.getTransactionValue("TemplateId");
 
     LogMessage = "SZA SzaHeaderCO.processFormRequest - TemplateId:" + TemplateId.toString();
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String IsDeptApproverRequired = (String)pageContext.getTransactionValue("IsDeptApproverRequired");
 
     LogMessage = "SZA SzaButtonsCO.processFormRequest - IsDeptApproverRequired:" + IsDeptApproverRequired;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String IsInvoiceUploadRequired = (String)pageContext.getTransactionValue("IsInvoiceUploadRequired");
 
     LogMessage = "SZA SzaButtonsCO.processFormRequest - IsInvoiceUploadRequired:" + IsInvoiceUploadRequired;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     GeneralInfoAMImpl generalinfoamimpl = (GeneralInfoAMImpl)pageContext.getRootApplicationModule().findApplicationModule("GeneralInfoAM");
 
     String TemplateName = generalinfoamimpl.getsTemplateName(TemplateId);
     LogMessage = "SZA SzaButtonsCO.processFormRequest - TemplateName:" + TemplateName;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
      
     if (("GeneralInformationPG".equalsIgnoreCase(CurrentPage)) && ("OIENavBar".equalsIgnoreCase(source)) && ("goto".equalsIgnoreCase(event)))
     {
       if (("Y".equalsIgnoreCase(IsDeptApproverRequired)) && ("".equalsIgnoreCase(SzaDeptApprover)))
       {
         MessageToken[] tokens = { new MessageToken("EXPENSE_REPORT_NUMBER", strExpenseReportNumber) };
 
         String Message = pageContext.getMessage("SQLSZA", "SZA_OIE_DEPT_APPOVER_REQUIRE", tokens);
 
         OAException confirmMessage = new OAException(Message, (byte)0);
 
         pageContext.putDialogMessage(confirmMessage);
 
         return;
       }
       Integer InvoiceUploadCount = SzaOIEUtil.getExpenseReportInvoiceUploadCount(pageContext, webBean, ReportHeaderId);
 
       LogMessage = "SZA SzaButtonsCO.processFormRequest - InvoiceUploadCount:" + InvoiceUploadCount.toString();
 
       SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
       if (("Y".equalsIgnoreCase(IsInvoiceUploadRequired)) && (InvoiceUploadCount.intValue() < 1))
       {
         MessageToken[] tokens = { new MessageToken("EXPENSE_TEMPLATE_NAME", TemplateName) };
 
         String Message = pageContext.getMessage("SQLSZA", "SZA_OIE_INVOICE_UPLOAD_REQUIRE", tokens);
 
         OAException confirmMessage = new OAException(Message, (byte)0);
 
         pageContext.putDialogMessage(confirmMessage);
 
         return;
       }
     }
     if (("CashAndOtherLinesPG".equalsIgnoreCase(CurrentPage)) && ("OIENavBar".equalsIgnoreCase(source)) && ("goto".equalsIgnoreCase(event)))
     {
       DecimalFormat df = new DecimalFormat("#,##0.00");
 
       Double LineTotalAmount = Double.valueOf(0.0D);
 
       ReceiptBasedLinesVOImpl LineVo = (ReceiptBasedLinesVOImpl)am.findViewObject("CashAndOtherLinesVO");
     
       if (LineVo != null)
       {
         LineVo.first();
 
         while (LineVo.hasNext())
         {
           ReceiptBasedLinesVORowImpl row = (ReceiptBasedLinesVORowImpl)LineVo.getCurrentRow();
 
           if (row.getAmount() != null)
           {
             LineTotalAmount = Double.valueOf(Arith.round(Arith.add(LineTotalAmount.doubleValue(), row.getAmount().getValue()), 2));
 
             LogMessage = "SZA SzaButtonsCO.processFormRequest - row.getAmount().getValue():" + row.getAmount().getValue();
 
             SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
           }
          
           LineVo.next();
         }
         LineVo.first();
 
         LogMessage = "SZA SzaButtonsCO.processFormRequest - LineTotalAmount:" + LineTotalAmount;
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         if ("Y".equalsIgnoreCase(IsExpenseRecognitionReport))
         {
           if (LineTotalAmount.doubleValue() != 0.0D)
           {
             MessageToken[] tokens = { new MessageToken("RECOGNITION_TOTA_AMOUNT", df.format(LineTotalAmount)), new MessageToken("EXPENSE_TEMPLATE_NAME", TemplateName) };
 
             String Message = pageContext.getMessage("SQLSZA", "SZA_OIE_RECOGNITION_TOTAL_ZERO", tokens);
 
             OAException confirmMessage = new OAException(Message, (byte)0);
 
             pageContext.putDialogMessage(confirmMessage);
 
             return;
           }
         }
         double InvoiceUploadTotalAmount = SzaOIEUtil.getInvoiceUploadTotalAmount((OADBTransactionImpl)am.getOADBTransaction(), ReportHeaderId);
 
         LogMessage = "SZA SzaButtonsCO.processFormRequest - InvoiceUploadTotalAmount:" + Double.toString(InvoiceUploadTotalAmount);
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         if (("Y".equalsIgnoreCase(IsInvoiceUploadRequired)) && (InvoiceUploadTotalAmount != LineTotalAmount.doubleValue()))
         {
           MessageToken[] tokens = { new MessageToken("EXPENSE_REPORT_TOTA_AMOUNT", df.format(LineTotalAmount)), new MessageToken("INVOICE_UPLOAD_TOTA_AMOUNT", df.format(InvoiceUploadTotalAmount)), new MessageToken("EXPENSE_TEMPLATE_NAME", TemplateName) };
 
           String Message = pageContext.getMessage("SQLSZA", "SZA_OIE_INV_TOTAL_AMT_NOT_EQ", tokens);
 
           OAException confirmMessage = new OAException(Message, (byte)0);
 
           pageContext.putDialogMessage(confirmMessage);
 
           return;
         }
       }
     }
     if ((pageContext.getParameter("OIESave") != null) || (event.equalsIgnoreCase("goto")))
     {
       if ((("CashAndOtherLinesPG".equalsIgnoreCase(CurrentPage)) && (event.equals(""))) || (CurrentPage.equals("GeneralInformationPG")))
       {
         LogMessage = "SZA \"SAVE\".equals(s1)) && (\"OIESave\".equals(s2) - Start";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         String BudgetCostCenterCode = "";
         String FlexConcatenated = "";
 
         OAViewObject HeaderVo = (OAViewObject)am.findViewObject("ExpenseReportHeadersVO");
 
         if (HeaderVo != null)
         {
           LogMessage = "SZA ExpenseReportHeadersVO - Start ";
 
           SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
           FlexConcatenated = (String)HeaderVo.getCurrentRow().getAttribute("FlexConcatenated");
 
           BudgetCostCenterCode = (String)HeaderVo.getCurrentRow().getAttribute("Attribute12");
 
           LogMessage = "SZA ExpenseReportHeadersVO - BudgetCostCenterCode :" + BudgetCostCenterCode;
 
           SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
           if ((BudgetCostCenterCode == null) || (BudgetCostCenterCode == ""))
           {
             BudgetCostCenterCode = FlexConcatenated;
             //add by xiezili
             HeaderVo.getCurrentRow().setAttribute("Attribute12",FlexConcatenated);
           }
           LogMessage = "SZA ExpenseReportHeadersVO - BudgetCostCenterCode :" + BudgetCostCenterCode;
 
           SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
         }
 
         LogMessage = "SZA ExpenseTypeTreeCO processRequest - BudgetCostCenterCode:" + BudgetCostCenterCode;
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         OAViewObject LineVo = (OAViewObject)am.findViewObject("CashAndOtherLinesVO");
         
         
         //add by xiezili 2012-12-13
          String templateType = isTravleTemplate(TemplateId,am);
         if("TRA".equals(templateType) || "BUDGET".equals(templateType)){
                 TravelExpenseCategoryVOImpl vo = (TravelExpenseCategoryVOImpl)am.findViewObject("TravelExpenseCategoryVO1");
                 if (vo == null)
                 {
                   vo = (TravelExpenseCategoryVOImpl)am.createViewObject("TravelExpenseCategoryVO1", "sza.oracle.apps.ap.oie.budget.server.TravelExpenseCategoryVO");
                 }
                 if (vo != null)
                 {
                   vo.executeQuery();
                 } else {
                 }
         } else {
         if (!"Y".equalsIgnoreCase(IsExpenseRecognitionReport))
         {
           if (LineVo != null)
           {
             LineVo.first();
 
             LogMessage = "SZA SzaButtonsCO.processFormRequest - Line Loop - Start";
 
             SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
             while (LineVo.hasNext())
             {
               Row row = LineVo.getCurrentRow();
 
               String ReportLineId = row.getAttribute("ReportLineId").toString();
 
               LogMessage = "SZA ExpenseTypeTreeCO.processTreeNodePost - RowReportLineId:" + ReportLineId;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               String GlAccount = "NULL";
               String BudgetGlAccount = "NULL";
               String ExpTypeTreeItemCode = "NULL";
               if (row.getAttribute("Attribute10") != null)
               {
                 ExpTypeTreeItemCode = row.getAttribute("Attribute10").toString();
               }
 
               LogMessage = "SZA SzaButtonsCO.processFormRequest - Before Replace - ExpTypeTreeItemCode:" + ExpTypeTreeItemCode;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               ExpTypeTreeItemCode = ExpTypeTreeItemCode.replaceAll("XXXXXXX", BudgetCostCenterCode);
 
               LogMessage = "SZA SzaButtonsCO.processFormRequest - After Replace - ExpTypeTreeItemCode:" + ExpTypeTreeItemCode;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               GlAccount = SzaOIEUtil.getGlAccount(pageContext, webBean, ExpTypeTreeItemCode);
 
               LogMessage = "SZA SzaButtonsCO.processFormRequest - Update Line VO - GlAccount = " + GlAccount;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               BudgetGlAccount = SzaOIEUtil.getBudgetGlAccount(pageContext, webBean, ExpTypeTreeItemCode);
 
               LogMessage = "SZA SzaButtonsCO.processFormRequest - Update Line VO - BudgetGlAccount = " + BudgetGlAccount;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               row.setAttribute("Attribute13", BudgetGlAccount);
 
               row.setAttribute("Attribute15", GlAccount);
               
               LogMessage = "SZA SzaButtonsCO.processFormRequest - Update Line VO - ReportLineId = " + ReportLineId;
 
               SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
               LineVo.next();
             }
             LineVo.reset();
 
             LogMessage = "SZA SzaButtonsCO.processFormRequest - Line Loop - End";
 
             SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
           }
 
         }
 
         LogMessage = "SZA \"SAVE\".equals(s1)) && (\"OIESave\".equals(s2) - End";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
       }
       }
 
     }
 
     if (("FinalReviewPG".equalsIgnoreCase(CurrentPage)) && ("".equalsIgnoreCase(source)) && ("".equalsIgnoreCase(event)) && (pageContext.getParameter("OIESubmit") != null))
     {
       LogMessage = "SZA pageContext.getParameter(\"OIESubmit\") != null - Start";
 
       SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
       String strExpenseReportHeaderID = intReportHeaderId.toString();
       String returnResultErrbuf = "";
       String returnResultRetcode = "";
 
       DBTransaction trans = pageContext.getApplicationModule(webBean).getOADBTransaction();
 
       String strSql = " BEGIN SZA_OIE_BUDGET_PKG.main(errbuf => :1,retcode => :2,p_report_header_id => :3); END; ";
 
       CallableStatement cs = trans.createCallableStatement(strSql, 1);
       try
       {
         LogMessage = "SZA Execute to get the budget control result - Try - Start";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         cs.setString(3, strExpenseReportHeaderID);
 
         ((OracleCallableStatement)cs).registerOutParameter(1, 12, 0, 2000);
 
         ((OracleCallableStatement)cs).registerOutParameter(2, 12, 0, 2000);
 
         cs.execute();
         returnResultErrbuf = cs.getString(1);
         returnResultRetcode = cs.getString(2);
 
         cs.close();
 
         LogMessage = "SZA Execute to get the budget control result - Try - End";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
       }
       catch (SQLException sqle)
       {
         LogMessage = "SZA Execute to get the budget control result - Catch - Start";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
         try
         {
           cs.close();
         }
         catch (Exception e)
         {
         }
 
         LogMessage = "SZA Execute to get the budget control result - Catch - End";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
       }
 
       LogMessage = "SZA returnResultRetcode : " + returnResultRetcode;
 
       SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
       LogMessage = "SZA returnResultErrbuf : " + returnResultErrbuf;
 
       SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
       if (("1".equals(returnResultRetcode)) || ("2".equals(returnResultRetcode)))
       {
         MessageToken[] tokens = { new MessageToken("EXPENSE_REPORT_NUMBER", strExpenseReportNumber) };
 
         String strConfrimMessage = pageContext.getMessage("SQLSZA", "SZA_OIE_BUDGET_OVER", tokens);
 
         strConfrimMessage = returnResultErrbuf;
         OAException confirmMessage = new OAException(strConfrimMessage, (byte)0);
 
         pageContext.putDialogMessage(confirmMessage);
 
         LogMessage = "SZA putDialogMessage(confirmMessage) ";
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         return;
       }
       LogMessage = "SZA pageContext.getParameter(\"OIESubmit\") != null - End";
 
       SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
     }
 
     super.processFormRequest(pageContext, webBean);
 
     LogMessage = "SZA SzaButtonsCO.processFormRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
   
     private String isTravleTemplate(String TemplateId, OAApplicationModule am) {
         String returnValue = "";
         String callStmt = 
             " begin SZA_OIE_BUDGET_UTIL.GET_REPORT_TEMPLATE_TYPE(p_expense_report_id => :1,\n" + 
             "                          o_return_value       => :2); end;";
         DBTransaction txn = am.getOADBTransaction();
         CallableStatement cs = null;
         Row row = null;
       
             try {
                 cs = txn.createCallableStatement(callStmt, 1);
                 cs.setString(1, TemplateId);
                 cs.setString(2, returnValue);
                 cs.registerOutParameter(2, Types.VARCHAR);
                 cs.execute();
                 returnValue = cs.getString(2);
                
                 cs.close();
             } catch (Exception e) {
                 e.printStackTrace();
                 try {
                     cs.close();
                 } catch (Exception ex) {

                 }
             }
             return returnValue;
     }
     
     private void setDefaultExpType(String TemplateId,String orgId, OAApplicationModule am) {
         String returnValue = "";
         String callStmt = 
             " begin SZA_OIE_BUDGET_UTIL.get_default_exp_data(p_org_id => :1,\n" + 
             "                          p_report_type_id       => :2," +
         "                          o_attribute1       => :3," +
         "                          o_attribute2       => :4," +
         "                          o_attribute11       => :5," +
         "                          o_attribute12       => :6," +
         "                          o_attribute13       => :7," +
         "                          o_attribute14       => :8," +
         "                          o_attribute15       => :9" +
             "                          ); end;";
         DBTransaction txn = am.getOADBTransaction();
         CallableStatement cs = null;
         Row row = null;
       
             try {
                 cs = txn.createCallableStatement(callStmt, 1);
                 cs.setString(1, orgId);
                 cs.setString(2, TemplateId);
                 cs.registerOutParameter(3, Types.VARCHAR);
                 cs.registerOutParameter(4, Types.VARCHAR);
                 cs.registerOutParameter(5, Types.VARCHAR);
                 cs.registerOutParameter(6, Types.VARCHAR);
                 cs.registerOutParameter(7, Types.VARCHAR);
                 cs.registerOutParameter(8, Types.VARCHAR);
                 cs.registerOutParameter(9, Types.VARCHAR);
                 cs.execute();
                 attribute1 = cs.getString(3);
                 attribute2 = cs.getString(4);
                 attribute11 = cs.getString(5);
                 attribute12 = cs.getString(6);
                 attribute13 = cs.getString(7);
                 attribute14 = cs.getString(8);
                 attribute15 = cs.getString(9);
                System.err.println("attribute13333:"+attribute13+"  attribute155555:"+attribute15);
                 cs.close();
             } catch (Exception e) {
                 e.printStackTrace();
                 try {
                     cs.close();
                 } catch (Exception ex) {

                 }
             }
     }
 }



